<template>
  <el-dialog :visible.sync="value" :before-close="cancel" append-to-body>
    <template slot="title">
      {{auditStep===1?'一次审核':'二次审核'}}
    </template>
    <el-form
      :model="form"
      :rules="auditRule"
      ref="auditForm"
      :label-width="formLabelWidth">
      <el-form-item label="账户名">
        <el-col :span="16">
          <el-input  @keyup.enter.native="submitForm('auditForm')" v-model="rowData.username" disabled="disabled" auto-complete="off"/>
        </el-col>
      </el-form-item>
      <el-form-item label="一审备注" prop="remark" v-if="auditStep===1 && current === 0">
        <el-col :span="16">
          <el-input  @keyup.enter.native="submitForm('auditForm')" type="textarea" v-model="form.remark" auto-complete="off"/>
        </el-col>
      </el-form-item>
      <div v-else>
        <el-form-item label="一审状态">
          <el-col :span="16">
            <el-input  @keyup.enter.native="submitForm('auditForm')" type="textarea" :value="firstTxt" disabled auto-complete="off"/>
          </el-col>
        </el-form-item>
        <el-form-item label="一审备注">
          <el-col :span="16">
            <el-input  @keyup.enter.native="submitForm('auditForm')" type="textarea" v-model="rowData.firstRemark" disabled auto-complete="off"/>
          </el-col>
        </el-form-item>
      </div>
      <el-form-item label="二审备注" v-if="auditStep===2 && current === 0" prop="remark">
        <el-col :span="16">
          <el-input  @keyup.enter.native="submitForm('auditForm')" type="textarea" v-model="form.remark" auto-complete="off"/>
        </el-col>
      </el-form-item>
      <div v-if="auditStep===2 && current === 1" >
        <el-form-item label="二审状态">
          <el-col :span="16">
            <el-input  @keyup.enter.native="submitForm('auditForm')" type="textarea" :value="secondTxt" disabled auto-complete="off"/>
          </el-col>
        </el-form-item>
        <el-form-item label="二审备注" v-if="auditStep===2 && current === 1" prop="secondRemark">
          <el-col :span="16">
            <el-input  @keyup.enter.native="submitForm('auditForm')" type="textarea" v-model="rowData.secondRemark" disabled auto-complete="off"/>
          </el-col>
        </el-form-item>
      </div>
      <el-form-item label="审核结果" v-if="current === 0" prop="status">
        <el-col :span="16">
          <el-radio-group v-model="form.status" size="medium">
            <el-radio-button :label="1" value="1">通过</el-radio-button>
            <el-radio-button :label="2" value="2">不通过</el-radio-button>
          </el-radio-group>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" v-if="current === 0">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submitForm('auditForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

  import {form} from '@/build'
  import {updateAgencyAudit} from '@/services/api/agency';

  export default {
    name: "audit-dialog",
    props: ['value', 'id', 'auditStep', 'rowData'],
    mixins: [form],
    data() {
      return {
        formLabelWidth: '180px',
        form: {
          step: this.auditStep,
          proxyId: this.rowData.userId,
          status: undefined,
          remark: undefined,
        },
        auditRule: {
          firstRemark: [
            {required: true, message: '一审备注不能为空', trigger: 'blur'},
          ],
          remark: [
            {required: true, message: '备注不能为空', trigger: 'blur'},
          ],
          secondRemark: [
            {required: true, message: '二审备注不能为空', trigger: 'blur'},
          ],
          status: [
            {required: true, message: '请确认审核结果', trigger: 'blur'},
          ],
        }
      }
    },
    computed: {
      current() {
        let _current = 0;
        if (this.auditStep == 1) {
          _current = this.rowData.status === 0 ? 0: 1
        }
        if(this.auditStep == 2){
          _current = this.rowData.status === 1 ? 0:1
        }
        return _current
      },
      firstTxt(){
        const {status}= this.rowData
        if(status==2){
          return '一审未通过'
        }
        return '一审通过';
      },
      secondTxt(){
        const {status}= this.rowData
        if(status==4){
          return '二审未通过'
        }
        return '二审通过';
      },
    },

    watch: {
      auditStep() {
        this.form.step = this.auditStep
      }
    },
    mounted() {
      this.bindSave(updateAgencyAudit, 'saveForm', 'form', {
        afterSuccess: () => {
          this.cancel();
          this.$parent.getAll();
        }
      });
    },
    methods: {
      cancel() {
        this.$emit('input', false);
      },
      submitForm(formName){
        this.doSave({action: 'saveForm', form: formName})
      }
    },

  }
</script>

<style lang="scss" scoped>

</style>
